﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace ZEFIT
{
    class MYSQLConnecter
    {
        static public MySqlConnection myConnection = new MySqlConnection();

        static public List<object> SelectRows(MySqlCommand myCommand)
        {
            List<object> objectList = new List<object>();
            try
            {
                if (myConnection == null) { throw new Exception("Kein ConnectionString vorhanden!"); }
                myConnection.Open();
                myCommand.Connection = myConnection;
                MySqlDataReader reader = myCommand.ExecuteReader();
                while (reader.Read())
                {
                    List<object> varList = new List<object>();
                    for (int i = 0; reader.FieldCount > i; i++)
                    {
                        varList.Add(reader[i]);
                    }
                    objectList.Add(varList);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                myCommand.Dispose();
                myCommand = null;
                myConnection.Close();
            }
            return objectList;
        }

        static public void Send(MySqlCommand myCommand)
        {
            try
            {
                if (myConnection == null) { throw new Exception("Kein ConnectionString vorhanden"); }
                myConnection.Open();
                myCommand.Connection = myConnection;
                int dbExistTest = myCommand.ExecuteNonQuery();
                if (dbExistTest <= 0) { throw new Exception("Datenbank nicht vorhanden"); }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                myCommand.Dispose();
                myCommand = null;
                myConnection.Close();
            }
        }

        static public int SendInsert(MySqlCommand myCommand)
        {
            int lastId = 0;
            myCommand.CommandText = myCommand.CommandText + " SELECT LAST_INSERT_ID();";
            try
            {
                if (myConnection == null) { throw new Exception("Kein ConnectionString vorhanden"); }
                myConnection.Open();
                myCommand.Connection = myConnection;
                MySqlDataReader reader = myCommand.ExecuteReader();
                if (reader != null && reader.Read())
                    lastId = reader.GetInt32(0);
                
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                myCommand.Dispose();
                myCommand = null;
                myConnection.Close();
            }
            return lastId;
        }
    }
}
